//
//  CompleteCell.m
//  ios-supplier
//
//  Created by feynmanyuan on 15/4/17.
//  Copyright (c) 2015年 feynmanyuan. All rights reserved.
//  绘制完成订单的Cell
//

#import "CompleteOrderCell.h"

@interface CompleteOrderCell(){
    
    UILabel *_orderCodeLabel;
    
    UILabel *_staffLabel;
    UILabel *_completeLabel;
    
    UILabel *_userNameLabel;
    UILabel *_addressLabel;
    
    UILabel *_paymentLabel;
    
    UILabel *_moneyLabel;
    
    UILabel *_totalCountLabel;
    
    UIView *_spacingView;
    
    UIView *_line1View;
    UIView *_line2View;
    UIView *_line3View;
}
@end

@implementation CompleteOrderCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier frame:(CGRect)frame{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier frame:frame];
    if (self) {
        [self initSubView];
    }
    return self;
}

-(void)initSubView{
    _spacingView = [[UIView alloc] init];
    [_spacingView setBackgroundColor:PanelBgColorDefault];
    [self addSubview:_spacingView];
    
    _orderCodeLabel = [[UILabel alloc] init];
    _orderCodeLabel.font = [UIFont systemFontOfSize:TextFontSizeH2];
    _orderCodeLabel.textColor = TextColorDefault;
    [self addSubview:_orderCodeLabel];
    
    
    _staffLabel = [[UILabel alloc] init];
    _staffLabel.font = [UIFont systemFontOfSize:TextFontSizeH3];
    _staffLabel.textColor = TextColorLight;
    [self addSubview:_staffLabel];
    
    _completeLabel = [[UILabel alloc] init];
    _completeLabel.font = [UIFont systemFontOfSize:TextFontSizeH3];
    _completeLabel.textColor = TextColorLight;
    [self addSubview:_completeLabel];
    
    _userNameLabel = [[UILabel alloc] init];
    _userNameLabel.font = [UIFont systemFontOfSize:TextFontSizeH3];
    _userNameLabel.textColor = TextColorLight;
    [self addSubview:_userNameLabel];
    
    _addressLabel = [[UILabel alloc] init];
    _addressLabel.font = [UIFont systemFontOfSize:TextFontSizeH3];
    _addressLabel.textColor = TextColorLight;
    _addressLabel.lineBreakMode = NSLineBreakByCharWrapping;
    _addressLabel.numberOfLines = 0;
    [self addSubview:_addressLabel];
    
    _paymentLabel = [[UILabel alloc] init];
    _paymentLabel.font = [UIFont systemFontOfSize:TextFontSizeH3];
    _paymentLabel.textColor = TextColorDefault;
    [self addSubview:_paymentLabel];
    
    _moneyLabel = [[UILabel alloc] init];
    _moneyLabel.font = [UIFont systemFontOfSize:TextFontSizeH3];
    _moneyLabel.textColor = TextColorImportant;
    [self addSubview:_moneyLabel];
    
    _totalCountLabel = [[UILabel alloc] init];
    _totalCountLabel.font = [UIFont systemFontOfSize:TextFontSizeH3];
    _totalCountLabel.textColor = TextColorLight;
    [self addSubview:_totalCountLabel];
    
    _line1View = [[UIView alloc] init];
    _line1View.backgroundColor = LineColorDefault ;
    [self addSubview: _line1View];
    
    _line2View = [[UIView alloc] init];
    _line2View.backgroundColor = LineColorDefault ;
    [self addSubview: _line2View];
    
    _line3View = [[UIView alloc] init];
    _line3View.backgroundColor = LineColorDefault ;
    [self addSubview: _line3View];
}

// 各种计算
-(void)setOrder:(JFBOrderDS *)order{
    NSString *orderCode = [NSString stringWithFormat:@"订单编号：%@", order.orderCode];
    CGSize orderCodeSize = [orderCode sizeWithAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:TextFontSizeH2]}];
    CGFloat orderCodeX = kOrderCellMargin, orderCodeY = (kOrderCellHeaderHeight - orderCodeSize.height) / 2;
    CGRect orderCodeRect = CGRectMake(orderCodeX, orderCodeY, orderCodeSize.width, orderCodeSize.height);
    _orderCodeLabel.text = orderCode;
    _orderCodeLabel.frame = orderCodeRect;

    _line1View.frame = CGRectMake(kOrderCellSpacing, kOrderCellHeaderHeight, self.bounds.size.width - kOrderCellSpacing * 2, 1);
    
    NSString *staff = [NSString stringWithFormat:@"配送人员：%@(%@)", order.staff, order.staffMobile];
    CGSize staffSize = [staff sizeWithAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:TextFontSizeH3]}];
    CGFloat staffX = kOrderCellMargin, staffY = kOrderCellHeaderHeight + kOrderCellSpacing;
    _staffLabel.width = [UIScreen mainScreen].bounds.size.width - 20;
    CGFloat staffW = staffSize.width > _staffLabel.width ? _staffLabel.width:staffSize.width;
    
    CGRect staffRect = CGRectMake(staffX, staffY, staffW, staffSize.height);
    _staffLabel.text = staff;
    _staffLabel.frame = staffRect;
    
    NSString *completeTime = [NSString stringWithFormat:@"收货时间：%@", order.completeAt];
    CGSize completeTimeSize = [completeTime sizeWithAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:TextFontSizeH3]}];
    CGFloat completeTimeX = kOrderCellMargin, completeTimeY = staffY + staffSize.height + kOrderCellSpacing;
    CGRect completeTimeRect = CGRectMake(completeTimeX, completeTimeY, completeTimeSize.width, completeTimeSize.height);
    _completeLabel.text = completeTime;
    _completeLabel.frame = completeTimeRect;

    _line2View.frame = CGRectMake(kOrderCellSpacing, completeTimeY + completeTimeSize.height + kOrderCellSpacing, self.bounds.size.width - kOrderCellSpacing * 2, 1);
    
    NSString *userName = [NSString stringWithFormat:@"%@（%@）", order.userName, order.userMobile];
    CGSize userNameSize = [userName sizeWithAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:TextFontSizeH3]}];
    CGFloat userNameY = completeTimeY + completeTimeSize.height + kOrderCellSpacing * 2;
    CGFloat userNameX = kOrderCellMargin;
    CGRect userNameRect = CGRectMake(userNameX, userNameY, userNameSize.width, userNameSize.height);
    _userNameLabel.text = userName;
    _userNameLabel.frame = userNameRect;
    
    NSString * address = [order.address isEqual:[NSNull null]] ? @"" : order.address;
//    CGSize addressSize = [address sizeWithAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:TextFontSizeH3]}];
    CGFloat addressY = userNameY + userNameSize.height + kOrderCellSpacing;
    CGFloat addressX = kOrderCellMargin;
    _addressLabel.text = address;
    CGSize addressSize = [_addressLabel sizeThatFits:CGSizeMake(self.bounds.size.width - kOrderCellSpacing * 2, MAXFLOAT)];
    CGRect addressRect = CGRectMake(addressX, addressY, addressSize.width, addressSize.height);
    _addressLabel.frame = addressRect;

    _line3View.frame = CGRectMake(kOrderCellSpacing, addressY + addressSize.height + kOrderCellSpacing, self.bounds.size.width - kOrderCellSpacing * 2, 1);
    
    CGSize paymentSize = [order.payment sizeWithAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:TextFontSizeH3]}];
    CGFloat paymentY = addressY + addressSize.height + kOrderCellSpacing + (kOrderCellHeaderHeight - paymentSize.height) / 2;
    CGFloat paymentX = kOrderCellMargin;
    CGRect paymentRect = CGRectMake(paymentX, paymentY, paymentSize.width, paymentSize.height);
    _paymentLabel.text = order.payment;
    _paymentLabel.frame = paymentRect;
    
    NSString *money = [NSString stringWithFormat:@"¥ %@", [NSString decimalToString:order.money]];
    CGSize moneySize = [money sizeWithAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:TextFontSizeH3]}];
    CGFloat moneyY = paymentY;
    CGFloat moneyX = self.bounds.size.width - kOrderCellSpacing - moneySize.width;
    CGRect moneyRect = CGRectMake(moneyX, moneyY, moneySize.width, moneySize.height);
    _moneyLabel.text = money;
    _moneyLabel.frame = moneyRect;
    
    NSString *totalCount = [NSString stringWithFormat:@"共计%ld个商品", (unsigned long)order.totalCount];
    CGSize totalCountSize = [totalCount sizeWithAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:TextFontSizeH3]}];
    CGFloat totalCountY = paymentY;
    CGFloat totalCountX = moneyX - kOrderCellSpacing - totalCountSize.width;
    CGRect totalCountRect = CGRectMake(totalCountX, totalCountY, totalCountSize.width, totalCountSize.height);
    _totalCountLabel.text = totalCount;
    _totalCountLabel.frame = totalCountRect;
    
    _spacingView.frame = CGRectMake(0, addressY + addressSize.height + kOrderCellSpacing + kOrderCellHeaderHeight, self.bounds.size.width, kOrderCellSpacing);
    
    self.height = CGRectGetMaxY(_spacingView.frame);
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
